home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / himetr1r / moddata.bas < prev    next >
Encoding:
BASIC Source File  |  1999-08-18  |  48.5 KB  |  1,771 lines

  1. Attribute VB_Name = "modData"
  2. '----------------------------------------
  3. '- Name: Sam Huggill
  4. '- Email: sam@vbsquare.com
  5. '- Web: http://www.vbsquare.com/
  6. '- Company: Lighthouse Internet Solutions
  7. '- Date/Time: 14/08/99 11:30:46
  8. '----------------------------------------
  9. '- Notes:   An interface between the project
  10. '           and the database
  11. '----------------------------------------
  12.  
  13. Option Explicit
  14.  
  15. ' Private Members of the form
  16. Private m_strCode As String     '// Code
  17. Private m_strNotes As String    '// Notes
  18. Private m_strExample As String  '// Example
  19. Private m_strDesc As String     '// Description
  20. Private m_strKey As String      '// Item ID
  21. Private m_strParentKey As String '// Parent ID
  22. Private m_strDate As String     '// Date of file
  23. Public m_strDBName As String   '// DB Name
  24.  
  25. Private m_db As Database        '// Database
  26. Private m_rs As Recordset       '// Recordset
  27. Private m_nodNode As Node       '// Current Node
  28. Private m_liItem As ListItem    '// Current List Item
  29.  
  30. ' Public Members
  31. Public g_strVersion As String   '// Version
  32. Public g_strLevel As String     '// Level
  33. Public g_blnRTF As Boolean      '// Include RTF Colouring
  34.  
  35. Private Const CHUNKSIZE As Long = 16384 ' internal chunksize
  36.  
  37. Public Sub FillTree(tvw As TreeView)
  38.     
  39.     Dim blnFolder As Boolean
  40.  
  41.     On Error GoTo vbErrHand
  42.     
  43. ' Fill tvw with the records in the database
  44.  
  45.     With tvw.Nodes
  46.         .Clear
  47.         .Add , , "ROOT", "Developers Code Book", "ROOT"
  48.     End With
  49.     
  50. ' If the database is only version 2, then don't try to add
  51. ' any folders
  52.  
  53.     If GetVersion = 2 Then GoTo FillCode
  54.  
  55.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders Order By ParentID", dbOpenSnapshot)
  56.  
  57.     If EmptyRS(m_rs) Then
  58.         GoTo FillCode
  59.     End If
  60.  
  61. ' Add the folders to the treeview
  62.  
  63.     m_rs.MoveFirst
  64.     With tvw.Nodes
  65.  
  66.         Do While Not m_rs.EOF
  67.             m_strParentKey = m_rs!ParentID
  68.             m_strKey = m_rs!Id
  69.             m_strDesc = "" & m_rs!Name
  70.             Set m_nodNode = tvw.Nodes.Add("ROOT", tvwChild, "F" & m_strKey, m_strDesc, "CLOSED")
  71.             m_nodNode.Tag = "F" & m_strParentKey
  72.             m_rs.MoveNext
  73.         Loop
  74.     End With
  75.  
  76.     m_rs.Close
  77.  
  78. FillCode:
  79.  
  80. ' Add the code items to the tree
  81.  
  82.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Code Order By ParentID", dbOpenSnapshot)
  83.  
  84.     If EmptyRS(m_rs) Then
  85.         If tvw.Nodes("ROOT") Is Nothing Then
  86.             tvw.Nodes.Add , , "ROOT", "Developers Code Book", "ROOT"
  87.             HighlightFolders tvw, "ROOT"
  88.         End If
  89.         GoTo CleanUp
  90.     End If
  91.  
  92.     m_rs.MoveFirst
  93.     
  94. ' Start adding the code items
  95.     
  96.     With tvw.Nodes
  97.  
  98.         Do While Not m_rs.EOF
  99.  
  100.             m_strParentKey = m_rs!ParentID
  101.             m_strKey = "" & m_rs!Id
  102.             m_strDesc = "" & m_rs!Description
  103.  
  104.             Set m_nodNode = tvw.Nodes.Add("ROOT", tvwChild, "C" & m_strKey, m_strDesc, "MODULE")
  105.             If GetVersion = 3 Then
  106.                 m_nodNode.Tag = "F" & m_strParentKey
  107.             Else
  108.                 m_nodNode.Tag = "C" & m_strParentKey
  109.             End If
  110.  
  111.             m_rs.MoveNext
  112.  
  113.         Loop
  114.  
  115.     End With
  116.     
  117. ' Now rebuild the tree structure based on the ParentID
  118.  
  119.     For Each m_nodNode In tvw.Nodes
  120.         m_strParentKey = m_nodNode.Tag
  121.  
  122.         If Len(m_strParentKey) > 0 Then
  123. ' Don't bother check which version here, just account for both
  124.             If m_strParentKey = "F0" Or m_strParentKey = "C0" Then
  125.                 m_strParentKey = "ROOT"
  126.             End If
  127.             Set m_nodNode.Parent = tvw.Nodes(m_strParentKey)
  128.         End If
  129.     Next
  130.  
  131. CleanUp:
  132.  
  133. ' Make the first level folders visilbe
  134.  
  135.     tvw.Nodes("ROOT").Expanded = True
  136.     tvw.Nodes("ROOT").Sorted = True
  137.     HighlightFolders tvw, "ROOT"
  138.     m_rs.Close
  139.  
  140. ' Go through the folders and sort them
  141. ' If Version 2, then set the appropriate icon
  142.  
  143.     For Each m_nodNode In tvw.Nodes
  144.         If Not m_nodNode.Key = "ROOT" Then
  145.             If m_nodNode.Children > 0 Then
  146.                 m_nodNode.Sorted = True
  147.                 If GetVersion = 2 Then
  148.                     m_nodNode.Image = "CLOSED"
  149.                 End If
  150.             End If
  151.         End If
  152.     Next
  153.  
  154.     Exit Sub
  155.  
  156. vbErrHand:
  157.     WriteError Err.Number, Err.Description, "FillTree", Now, App.Path & "\err.log"
  158.     MsgBox Err.Description, vbCritical + vbOKOnly, "FillTree"
  159.  
  160. End Sub
  161.  
  162. Private Function EmptyRS(rs As Recordset) As Boolean
  163.  
  164. ' Returns True if rs is empty
  165.  
  166.     EmptyRS = ((rs.BOF = True) And (rs.EOF = True))
  167.     
  168. End Function
  169.  
  170. Public Function OpenDB(ByVal strDBPath As String) As Boolean
  171.         
  172.     On Error GoTo vbErrHand
  173.     
  174. ' Returns True if successful
  175.  
  176. ' Check whether or not a filename has been passed
  177. ' If not, ask the user to open a new database
  178.  
  179.     If strDBPath = "" Then
  180.         strDBPath = frmMain.ShowFileDialog(eOpen, "", "Open Database")
  181.         If strDBPath = "" Then MsgBox "No DB Selected. Ending program.": OpenDB = False: Unload frmMain
  182.     End If
  183.  
  184. ' Open the DB and Set the m_db variable
  185.  
  186.     Set m_db = OpenDatabase(strDBPath)
  187.     m_strDBName = strDBPath
  188.     OpenDB = True
  189.  
  190.     Exit Function
  191.  
  192. vbErrHand:
  193.     WriteError Err.Number, Err.Description, "OpenDB", Now, App.Path & "\err.log"
  194.     MsgBox Err.Description, vbCritical + vbOKOnly, "OpenDB"
  195.     Resume Next
  196. End Function
  197.  
  198. Public Function SelectItem(ByVal strKey As String, ctl As Object) As String
  199.  
  200.     On Error GoTo vbErrHand
  201.  
  202.     Dim intKey As Integer
  203.     Dim blnFolder As Boolean
  204.     
  205. ' Select the appropriate data for an item
  206.  
  207.     If strKey = "ROOT" Then ctl.Code = "": ctl.Notes = "": ctl.Example = "": ctl.Caption = "Developers Code Book"
  208.  
  209. ' Check if the item is a folder object
  210.  
  211.     blnFolder = InStr(strKey, "F")
  212.  
  213.     If strKey <> "ROOT" Then
  214.  
  215.         intKey = Right(strKey, Len(strKey) - 1)
  216.         
  217. ' Open the correct table according the whether or not the item is a folder
  218.         
  219.         If blnFolder Then
  220.             Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders WHERE ID =" & intKey, dbOpenDynaset)
  221.         Else
  222.             Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID =" & intKey, dbOpenDynaset)
  223.         End If
  224.  
  225.         If Not EmptyRS(m_rs) Then
  226.             If blnFolder Then
  227.             
  228.                 With m_rs
  229.                     m_strCode = ""
  230.                     m_strExample = ""
  231.                     m_strNotes = ""
  232.                     m_strDesc = "" & .Fields("Name").Value
  233.                     g_strLevel = ""
  234.                     g_strVersion = ""
  235.                     .Close
  236.                 End With
  237.                 
  238.                 ctl.Code = m_strCode
  239.                 ctl.Example = m_strExample
  240.                 ctl.Notes = m_strNotes
  241.                 ctl.Caption = m_strDesc
  242.             Else
  243.                 
  244.                 With m_rs
  245.                     m_strCode = "" & .Fields("Code").Value
  246.                     m_strExample = "" & .Fields("Example").Value
  247.                     m_strDesc = "" & .Fields("Description").Value
  248.                     m_strNotes = "" & .Fields("Notes").Value
  249.                     g_strLevel = "" & .Fields("Level").Value
  250.                     g_strVersion = "" & .Fields("Version").Value
  251.                     .Close
  252.                 End With
  253.                 
  254.                 ctl.Code = m_strCode
  255.                 ctl.Example = m_strExample
  256.                 ctl.Notes = m_strNotes
  257.                 ctl.Caption = m_strDesc
  258.             End If
  259.  
  260.         End If
  261.     End If
  262.     
  263. ' Return the name of the item
  264.     
  265.     SelectItem = m_strDesc
  266.     frmMain.Caption = "Developers Code Book: " & m_strDesc
  267.     
  268.     If GetVersion = 3 Then frmMain.tbrMain.ButtonEnabled("NEW") = InStr(strKey, "F")
  269.     
  270.     If frmMain.tbrMain.ButtonEnabled("NEW") = False And strKey = "ROOT" Then frmMain.tbrMain.ButtonEnabled("NEW") = True
  271.     ctl.Details True
  272.  
  273.     If InStr(strKey, "F") Then ctl.Details False
  274.  
  275.  
  276.     Exit Function
  277.  
  278. vbErrHand:
  279.     WriteError Err.Number, Err.Description, "SelectItem", Now, App.Path & "\err.log"
  280.     MsgBox Err.Description, vbCritical + vbOKOnly, "SelectItem"
  281.  
  282. End Function
  283.  
  284. Sub BackupDatabase(sDBPath As String, sOutput As String)
  285.  
  286.     On Error GoTo vbErrHand
  287.  
  288. ' If the backup file exists, then delete it
  289.  
  290.     If Len(Dir$(sOutput)) > 0 Then
  291.         Kill sOutput
  292.     End If
  293.  
  294. ' Compact the db to the new path
  295.  
  296.     DBEngine.CompactDatabase sDBPath, sOutput
  297.  
  298.     Exit Sub
  299.  
  300. vbErrHand:
  301.     WriteError Err.Number, Err.Description, "BackupDatabase", Now, App.Path & "\err.log"
  302.     MsgBox Err.Description, vbCritical + vbOKOnly, "BackupDatabase"
  303.  
  304. End Sub
  305.  
  306. Sub CompactDatabase(sDBName As String, sBackup As String)
  307.  
  308.     On Error GoTo vbErrHand
  309.  
  310. ' Check if the temp file exists and delete it
  311.  
  312.     If Len(Dir$(sBackup)) > 0 Then
  313.         Kill sBackup
  314.     End If
  315.  
  316. ' Compact the database to a temp file
  317.  
  318.     DBEngine.CompactDatabase sDBName, sBackup
  319.  
  320. ' Delete the current database
  321.  
  322.     Kill sDBName
  323.  
  324. ' Restore the compacted temp database to the current one
  325.  
  326.     DBEngine.CompactDatabase sBackup, sDBName
  327.  
  328. ' Delete the temp database
  329.  
  330.     Kill sBackup
  331.  
  332.     Exit Sub
  333.  
  334. vbErrHand:
  335.     WriteError Err.Number, Err.Description, "CompactDatabase", Now, App.Path & "\err.log"
  336.     MsgBox Err.Description, vbCritical + vbOKOnly, "CompactDatabase"
  337.  
  338. End Sub
  339.  
  340. Sub RepairDatabase(sDBName As String)
  341.  
  342.     On Error GoTo vbErrHand
  343.  
  344. ' Repair the database
  345.     
  346.     DBEngine.RepairDatabase sDBName
  347.  
  348.     Exit Sub
  349.  
  350. vbErrHand:
  351.     WriteError Err.Number, Err.Description, "RepairDatabase", Now, App.Path & "\err.log"
  352.     MsgBox Err.Description, vbCritical + vbOKOnly, "RepairDatabase"
  353.  
  354. End Sub
  355.  
  356. Public Sub UpdateDB(tvw As TreeView)
  357.  
  358.     On Error GoTo vbErrHand
  359.  
  360.     Dim intID As Integer
  361.     Dim blnFolder As Boolean
  362.  
  363. ' Update the database values for the name
  364. ' Useful when label edits take place in the tvw
  365.  
  366.     Set m_nodNode = tvw.SelectedItem
  367.     If m_nodNode.Key = "ROOT" Then Exit Sub
  368.  
  369. ' Set the internal key and get the ID value
  370.  
  371.     m_strKey = m_nodNode.Key
  372.     intID = Right$(m_strKey, Len(m_strKey) - 1)
  373.     
  374. ' Check if the item is a folder, and open the correct table
  375.  
  376.     blnFolder = InStr(m_strKey, "F")
  377.     
  378.     If blnFolder Then
  379.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders WHERE ID =" & intID, dbOpenDynaset)
  380.     Else
  381.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID =" & intID, dbOpenDynaset)
  382.     End If
  383.     
  384. ' Update the table
  385.     
  386.     If blnFolder Then
  387.         With m_rs
  388.             .Edit
  389.             .Fields("Name").Value = m_strDesc
  390.             .Update
  391.             .Close
  392.         End With
  393.     Else
  394.  
  395.         With m_rs
  396.             .Edit
  397.             .Fields("Description").Value = m_strDesc
  398.             .Fields("Code").AppendChunk m_strCode
  399.             .Fields("Example").AppendChunk m_strExample
  400.             .Fields("Notes").AppendChunk m_strNotes
  401.             
  402.             If g_strVersion <> "" Then
  403.                 .Fields("Version").Value = g_strVersion
  404.             End If
  405.             
  406.             If g_strLevel <> "" Then
  407.                 .Fields("Level").Value = g_strLevel
  408.             End If
  409.             
  410.             .Update
  411.             .Close
  412.         End With
  413.     End If
  414.     
  415.     Exit Sub
  416.  
  417. vbErrHand:
  418.     WriteError Err.Number, Err.Description, "UpdateDB", Now, App.Path & "\err.log"
  419.     MsgBox Err.Description, vbCritical + vbOKOnly, "UpdateDB"
  420. End Sub
  421.  
  422. Public Property Let Code(ByVal New_Code As String)
  423.     m_strCode = New_Code
  424. End Property
  425.  
  426. Public Property Let Key(ByVal New_Key As String)
  427.     m_strKey = New_Key
  428. End Property
  429.  
  430. Public Property Get Key() As String
  431.     Key = m_strKey
  432. End Property
  433.  
  434. Public Property Let Notes(ByVal New_Notes As String)
  435.     m_strNotes = New_Notes
  436. End Property
  437.  
  438. Public Property Let Example(ByVal New_Example As String)
  439.     m_strExample = New_Example
  440. End Property
  441.  
  442. Public Property Let Description(ByVal New_Description As String)
  443.     m_strDesc = New_Description
  444. End Property
  445.  
  446. Public Property Get Description() As String
  447.     Description = m_strDesc
  448. End Property
  449.  
  450. Public Sub LoadBookmarks(lv As ListView)
  451.  
  452.     On Error GoTo vbErrHand
  453.  
  454.     Dim blnFolder As Boolean
  455.     lv.ListItems.Clear
  456.  
  457. ' Load the bookmarks into the listview
  458.  
  459.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Bookmarks", dbOpenDynaset)
  460.  
  461.     If Not EmptyRS(m_rs) Then
  462.         m_rs.MoveFirst
  463.         
  464. ' If the item is a folder then make sure its key is prefixed
  465. ' by an F
  466.  
  467.         Do While Not m_rs.EOF
  468.         If GetVersion = 3 Then
  469.             blnFolder = m_rs!IsFolder
  470.         Else
  471.             blnFolder = False
  472.         End If
  473.             
  474.             m_strKey = Trim$(Str(m_rs!CodeID))
  475.             Set m_liItem = lv.ListItems.Add(, , m_rs!Description)
  476.             
  477.             If blnFolder Then
  478.                 m_liItem.Key = "F" & m_strKey
  479.             Else
  480.                 m_liItem.Key = "C" & m_strKey
  481.             End If
  482.             
  483.             m_liItem.SubItems(1) = "" & m_rs!Section
  484.  
  485.             m_rs.MoveNext
  486.         Loop
  487.     End If
  488.     m_rs.Close
  489.     
  490.     Exit Sub
  491.  
  492. vbErrHand:
  493.     WriteError Err.Number, Err.Description, "LoadBookmarks", Now, App.Path & "\err.log"
  494.     MsgBox Err.Description, vbCritical + vbOKOnly, "LoadBookmarks"
  495. End Sub
  496.  
  497. Public Function SortBy(strType As String, strValue As String, lv As ListView) As String
  498.  
  499.     On Error GoTo vbErrHand
  500.  
  501. ' Load the Sort information
  502.  
  503.     lv.ListItems.Clear
  504.  
  505.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Code", dbOpenSnapshot)
  506.  
  507.     If Not EmptyRS(m_rs) Then
  508.         m_rs.MoveFirst
  509.         Do While Not m_rs.EOF
  510.         
  511.             If strType = "Version" Then
  512.             
  513.                 If m_rs!Version = strValue Then
  514.                 
  515.                     Set m_liItem = lv.ListItems.Add(, , m_rs!Description)
  516.                     m_liItem.Key = "C" & m_rs!Id
  517.                     
  518.                 End If
  519.                 
  520.             End If
  521.             
  522.             If strType = "Level" Then
  523.             
  524.                 If m_rs!Level = strValue Then
  525.                 
  526.                     Set m_liItem = lv.ListItems.Add(, , m_rs!Description)
  527.                     m_liItem.Key = "C" & m_rs!Id
  528.                     
  529.                 End If
  530.                 
  531.             End If
  532.             
  533.             m_rs.MoveNext
  534.         Loop
  535.     End If
  536.     
  537.     m_rs.Close
  538.  
  539.     If strType = "Version" Then
  540.         SortBy = "Sort by Version: " & strValue
  541.     End If
  542.  
  543.     If strType = "Level" Then
  544.         SortBy = "Sort by Level: " & strValue
  545.     End If
  546.  
  547.     Exit Function
  548.  
  549. vbErrHand:
  550.     WriteError Err.Number, Err.Description, "SortBy", Now, App.Path & "\err.log"
  551.     MsgBox Err.Description, vbCritical + vbOKOnly, "SortBy"
  552. End Function
  553.  
  554.  
  555. Public Sub RecursiveExportCode(nNode As Node, ByVal iFileNumber As Integer, ctl As Object)
  556.     
  557.     Dim nNodeChild As Node
  558.     Dim intIndex As Integer
  559.     Dim blnFolder As Boolean
  560.     Dim strKey As String
  561.     Dim oExport As FileDetails
  562.     
  563. ' Recursively Export Node Items by Chris Eastwood
  564.  
  565.     strKey = nNode.Key
  566.  
  567.     On Error Resume Next
  568.  
  569.     On Error GoTo 0
  570.     '// Get Details for item (as long as it's not the Root Item)
  571.     If StrComp(strKey, "ROOT", vbTextCompare) <> 0 Then
  572.         blnFolder = InStr(strKey, "F")
  573.         strKey = Right$(strKey, Len(strKey) - 1)
  574.         If blnFolder Then
  575.             Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders WHERE ID =" & strKey, dbOpenSnapshot)
  576.         Else
  577.             Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID =" & strKey, dbOpenSnapshot)
  578.         End If
  579.         oExport.sOldID = m_rs!Id
  580.         oExport.bFolder = blnFolder
  581.         oExport.sParentID = m_rs!ParentID
  582.         If blnFolder Then
  583.             oExport.sName = m_rs!Name
  584.         Else
  585.             oExport.sName = m_rs!Description
  586.             oExport.sNotes = "" & m_rs!Notes
  587.             oExport.sLevel = "" & m_rs!Level
  588.             oExport.sVersion = "" & m_rs!Version
  589.             frmMain.ctlData1.Code = "" & m_rs!Code
  590.             If g_blnRTF Then
  591.                 oExport.sCode = ctl.Code
  592.             Else
  593.                 oExport.sCode = ctl.PlainCode
  594.             End If
  595.             oExport.sExample = "" & m_rs!Example
  596.         End If
  597.         oExport.sParentName = nNode.Parent.Key
  598.  
  599.         Put #iFileNumber, , oExport
  600.  
  601.     End If
  602.  
  603.     '    m_rs.Close
  604.  
  605.     Set nNodeChild = nNode.Child
  606.     '// Now walk through the current parent node's children
  607.     Do While Not (nNodeChild Is Nothing)
  608.         '// If the current child node has it's own children...
  609.         RecursiveExportCode nNodeChild, iFileNumber, ctl
  610.         '// Get the current child node's next sibling
  611.         Set nNodeChild = nNodeChild.Next
  612.     Loop
  613. End Sub
  614.  
  615. Private Sub RecursiveDelete(oNode As Node, tvw As TreeView)
  616.     Dim nNodeChild As Node
  617.     Dim intID As Integer
  618.     Dim blnFolder As Boolean
  619.  
  620.     On Error GoTo vbErrHand
  621.     '// Recursivly delete nodes By Chris Eastwood
  622.     '// Get the items ID
  623.     intID = Right(m_strKey, Len(m_strKey) - 1)
  624.     blnFolder = InStr(m_strKey, "F")
  625.     '// Open the RS
  626.     If blnFolder Then
  627.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders WHERE ID =" & intID, dbOpenDynaset)
  628.     Else
  629.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID = " & intID, dbOpenDynaset)
  630.     End If
  631.  
  632.     With m_rs
  633.         .Delete
  634.         .Close
  635.     End With
  636.     '// Delete the nodes children
  637.     Set nNodeChild = oNode.Child
  638.     Do While Not (nNodeChild Is Nothing)
  639.         m_strKey = nNodeChild.Key
  640.         intID = Right(m_strKey, Len(m_strKey) - 1)
  641.         RecursiveDelete nNodeChild, tvw
  642.         Set nNodeChild = nNodeChild.Next
  643.     Loop
  644.  
  645.     Exit Sub
  646.  
  647. vbErrHand:
  648.     WriteError Err.Number, Err.Description, "RecursiveDelete", Now, App.Path & "\err.log"
  649.     MsgBox Err.Description, vbCritical + vbOKOnly, "RecursiveDelete"
  650.  
  651. End Sub
  652.  
  653. Public Sub DoActions()
  654.  
  655.     Dim blnBackup As Boolean
  656.     Dim blnCompact As Boolean
  657.     Dim blnRepair As Boolean
  658.     '// Compact, repair and backup
  659.     If Not (m_db Is Nothing) Then
  660.         m_db.Close
  661.         Set m_db = Nothing
  662.     End If
  663.  
  664.     blnBackup = Val(GetSetting(ThisApp, "Database", "Backup", 0))
  665.     blnCompact = Val(GetSetting(ThisApp, "Database", "Compact", 0))
  666.     blnRepair = Val(GetSetting(ThisApp, "Database", "Repair", 0))
  667.  
  668.     If blnBackup Then Call BackupDatabase(DBName, App.Path & "\backup.mdb")
  669.     DoEvents
  670.     If blnCompact Then Call CompactDatabase(DBName, App.Path & "\temp.mdb")
  671.     DoEvents
  672.     If blnRepair Then Call RepairDatabase(DBName)
  673.  
  674.     OpenDB DBName
  675.  
  676. End Sub
  677.  
  678. Public Sub ShowDetails(tvw As TreeView, lv As ListView)
  679.  
  680.     On Error GoTo vbErrHand
  681.  
  682.     '// Load a folders details into lv
  683.     Dim intID As Integer
  684.     Dim mID As Integer
  685.     Dim mrs As Recordset
  686.  
  687.     On Error Resume Next
  688.  
  689.     lv.ListItems.Clear
  690.  
  691.     If tvw.Nodes(m_strKey).Children > 0 Then '// Item has children
  692.         '// Get the items ID
  693.         intID = Val(Right$(m_strKey, Len(m_strKey) - 1))
  694.         '// Open the RS
  695.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Code Order By ParentID", dbOpenSnapshot)
  696.         If Not EmptyRS(m_rs) Then
  697.             m_rs.MoveFirst
  698.             Do While Not m_rs.EOF
  699.                 If m_rs!ParentID = intID Then '// Is a child of our item
  700.                     Set m_liItem = lv.ListItems.Add(, , m_rs!Description)
  701.                     m_liItem.Key = "C" & m_rs!Id
  702.                     mID = m_rs!Id
  703.                     Set mrs = m_db.OpenRecordset("SELECT * FROM Links WHERE CodeID =" & mID, dbOpenDynaset)
  704.                     '// Linkname still to be finished!
  705.                     m_liItem.SubItems(1) = mrs!LinkName
  706.                     '// Created still to be finsihed!
  707.                     m_liItem.SubItems(2) = mrs!Created
  708.                     mrs.Close
  709.                 End If
  710.                 m_rs.MoveNext
  711.             Loop
  712.         End If
  713.         m_rs.Close
  714.     Else
  715.         '// Item has no children
  716.     End If
  717.  
  718.     Exit Sub
  719.  
  720. vbErrHand:
  721.     WriteError Err.Number, Err.Description, "ShowDetails", Now, App.Path & "\err.log"
  722.     MsgBox Err.Description, vbCritical + vbOKOnly, "ShowDetails"
  723. End Sub
  724.  
  725. Public Sub SetNothing()
  726.     '// Free up memory on unload
  727.     If Not (m_rs Is Nothing) Then
  728.         Set m_rs = Nothing
  729.     End If
  730.  
  731.     If Not (m_db Is Nothing) Then
  732.         m_db.Close
  733.         Set m_db = Nothing
  734.     End If
  735.  
  736.     Set m_liItem = Nothing
  737.     Set m_nodNode = Nothing
  738. End Sub
  739.  
  740. Public Sub CompareCode(tvw As TreeView, strCode As String, strField As String)
  741.     Dim intID As Integer
  742.     '// Makes a judgement whether to update code or not
  743.     If tvw.SelectedItem Is Nothing Then Exit Sub
  744.  
  745.     m_strKey = tvw.SelectedItem.Key
  746.     If m_strKey = "ROOT" Then Exit Sub
  747.     '// Get the items ID
  748.     intID = Right(m_strKey, Len(m_strKey) - 1)
  749.     '// Open the RS
  750.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID =" & intID, dbOpenDynaset)
  751.  
  752.     If m_rs.Fields(strField).Value & "" <> strCode Then
  753.         Select Case strField
  754.             Case "Code"
  755.                 m_strCode = strCode
  756.             Case "Notes"
  757.                 m_strNotes = strCode
  758.             Case "Example"
  759.                 m_strExample = strCode
  760.             Case Else
  761.         End Select
  762.         UpdateDB tvw
  763.     End If
  764.  
  765.     '    m_rs.Close
  766. End Sub
  767.  
  768. Public Sub DeleteFavourite(lv As ListView, strKey As String)
  769.     Dim intIndex As Integer
  770.     Dim intCount As Integer
  771.     Dim intID As Integer
  772.     '// Deletes a favourite from the db and lv
  773.     For intCount = 1 To lv.ListItems.Count
  774.         If lv.ListItems(intCount).Key = strKey Then
  775.             intIndex = intCount
  776.             Exit For
  777.         End If
  778.     Next intCount
  779.     '// Get the items ID
  780.     intID = Right(strKey, Len(strKey) - 1)
  781.  
  782.     If intIndex > 0 Then
  783.         '// Open the RS
  784.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Bookmarks WHERE CodeID = " & Str$(intID), dbOpenDynaset)
  785.         If Not EmptyRS(m_rs) Then
  786.             With m_rs
  787.                 '// Delete the item
  788.                 .Delete
  789.                 .Close
  790.             End With
  791.         End If
  792.         '// Remove it from the lv
  793.         lv.ListItems.Remove intIndex
  794.     End If
  795.  
  796. End Sub
  797.  
  798. Public Function AddCode(tvw As TreeView, sName As String, sNotes As String, strVer As String, strLevel As String, blnSame As Boolean) As Boolean
  799.     Dim sParentKey As String
  800.     Dim intID As Integer
  801.     Dim sTitle As String
  802.     '// Adds a code item to the tree and the db
  803.     AddCode = False
  804.  
  805.     On Error GoTo vbErrHand
  806.  
  807.     If sName = "" Then MsgBox "Please enter a title.": Exit Function
  808.     '// Determines whether or not to add the item
  809.     '// at the same level or below it
  810.     If blnSame Then
  811.         Set m_nodNode = tvw.SelectedItem.Parent
  812.     Else
  813.         Set m_nodNode = tvw.SelectedItem
  814.     End If
  815.     '// Should not happen but check anyway
  816.     If m_nodNode Is Nothing Then
  817.         MsgBox "No item selected.", vbOKOnly + vbInformation
  818.         AddCode = False
  819.         AddCode = True
  820.         Exit Function
  821.     End If
  822.  
  823.     If m_nodNode.Key = "ROOT" Then
  824.         sParentKey = "0"
  825.     Else
  826.         sParentKey = Right$(m_nodNode.Key, Len(m_nodNode.Key) - 1)
  827.     End If
  828.     '// Open the RS
  829.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Code Where ID = 0", dbOpenDynaset)
  830.  
  831.     sTitle = sName
  832.  
  833.     '// Add and update the necessary fields
  834.     With m_rs
  835.         .AddNew
  836.         .Fields("Description").Value = sTitle
  837.         .Fields("ParentID").Value = sParentKey '// nNode.Index
  838.         .Fields("Code").AppendChunk m_strCode
  839.         .Fields("Notes").AppendChunk sNotes
  840.         .Fields("Version").Value = strVer
  841.         .Fields("Level").Value = strLevel
  842.         .Update
  843.         .Bookmark = .LastModified
  844.         intID = .Fields("ID")
  845.         .Close
  846.     End With
  847.  
  848.     If blnSame Then
  849.         Set m_nodNode = tvw.Nodes.Add(tvw.SelectedItem.Parent, tvwChild, "C" & intID, sTitle, "MODULE")
  850.     Else
  851.         Set m_nodNode = tvw.Nodes.Add(tvw.SelectedItem, tvwChild, "C" & intID, sTitle, "MODULE")
  852.     End If
  853.  
  854.     With tvw
  855.         .Nodes(m_nodNode.Key).EnsureVisible
  856.         .SelectedItem = .Nodes(m_nodNode.Key)
  857.     End With
  858.     '// Select the new item
  859.     SelectItem m_nodNode.Key, frmMain.ctlData1
  860.  
  861.     AddCode = True
  862.  
  863.     frmMain.ctlData1.CountItems tvw
  864.  
  865.     Exit Function
  866.  
  867. vbErrHand:
  868.     WriteError Err.Number, Err.Description, "AddCode", Now, App.Path & "\err.log"
  869.     MsgBox Err.Description, vbCritical + vbOKOnly, "AddCode"
  870.  
  871. End Function
  872.  
  873. Public Sub DeleteNode(tvw As TreeView)
  874.     '// Deletes an item from tvw and the db
  875.     Dim ret As Long
  876.     Dim Msg As String
  877.     Dim blnFolder As Boolean
  878.  
  879.     On Error GoTo vbErrHand
  880.  
  881.     '// Set the node handler and check if it is the root
  882.     Set m_nodNode = tvw.SelectedItem
  883.     If m_nodNode Is Nothing Then
  884.         MsgBox "No item selected.", vbOKOnly + vbInformation
  885.         Exit Sub
  886.     End If
  887.  
  888.     m_strKey = m_nodNode.Key
  889.     If m_strKey = "ROOT" Then Exit Sub
  890.     blnFolder = InStr(m_strKey, "F")
  891.     '// Check for any children and tell the user
  892.     If m_nodNode.Children > 0 Then
  893.         Msg = "Are you sure you want to delete this folder and all its children?"
  894.     Else
  895.         Msg = "Are you sure you want to delete this item?"
  896.     End If
  897.  
  898.     ret = MsgBox(Msg, vbExclamation + vbYesNo)
  899.     If ret = vbNo Then Exit Sub
  900.  
  901.     '// Recursivly delete the nodes children and update the
  902.     '// bookmarks control
  903.  
  904.     RecursiveDelete m_nodNode, tvw
  905.     m_strKey = tvw.SelectedItem.Key
  906.  
  907.     frmMain.ctlFavourites1.DeleteItem tvw
  908.     tvw.Nodes.Remove m_strKey
  909.  
  910.     frmMain.ctlData1.Code = ""
  911.     frmMain.ctlData1.Caption = ""
  912.     frmMain.ctlData1.Notes = ""
  913.     frmMain.ctlData1.Example = ""
  914.     frmMain.ctlData1.CountItems tvw
  915.  
  916.     If tvw.SelectedItem.Children = 0 And tvw.SelectedItem.Image = "OPEN" Then tvw.SelectedItem.Image = "MODULE"
  917.     '// Select the item
  918.     SelectItem tvw.SelectedItem.Key, frmMain.ctlData1
  919.  
  920.     Exit Sub
  921.  
  922. vbErrHand:
  923.     WriteError Err.Number, Err.Description, "DeleteNode", Now, App.Path & "\err.log"
  924.     MsgBox Err.Description, vbCritical + vbOKOnly, "DeleteNode"
  925.  
  926.  
  927. End Sub
  928.  
  929. Public Sub AddFavourite(tvw As TreeView, lv As ListView)
  930.     Dim intIndex As Integer
  931.     Dim intID As Integer
  932.     Dim blnFolder As Boolean
  933.  
  934.     '// Adds a favourite to lv and the db
  935.     If m_strKey = "ROOT" Then
  936.         MsgBox "Cannot make the root item a favourite."
  937.         Exit Sub
  938.     End If
  939.  
  940.     m_strKey = tvw.SelectedItem.Key
  941.     '// Get the items ID
  942.     intID = Right(m_strKey, Len(m_strKey) - 1)
  943.     blnFolder = InStr(m_strKey, "F")
  944.     Set m_nodNode = tvw.Nodes(m_strKey)
  945.     '// Check if the item is in lv
  946.     For intIndex = 1 To lv.ListItems.Count
  947.         If blnFolder Then
  948.             If lv.ListItems(intIndex).Key = "F" & intID Then
  949.                 MsgBox "Item already added."
  950.                 Exit Sub
  951.             End If
  952.         Else
  953.             If lv.ListItems(intIndex).Key = "C" & intID Then
  954.                 MsgBox "Item already added."
  955.                 Exit Sub
  956.             End If
  957.         End If
  958.     Next intIndex
  959.     '// Add the item
  960.     With lv
  961.  
  962.         Set m_liItem = .ListItems.Add(, , m_nodNode.Text)
  963.         m_liItem.Key = m_strKey
  964.         m_liItem.SubItems(1) = m_nodNode.Parent.Text
  965.  
  966.     End With
  967.  
  968.     '// Open and update the rs
  969.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Bookmarks", dbOpenDynaset)
  970.  
  971.     intID = Right$(m_nodNode.Key, Len(m_nodNode.Key) - 1)
  972.  
  973.     With m_rs
  974.         .AddNew
  975.         .Fields("CodeID").Value = intID
  976.         .Fields("Description").Value = m_nodNode.Text
  977.         .Fields("Section").Value = m_nodNode.Parent.Text
  978.         If GetVersion = 3 Then
  979.             .Fields("IsFolder").Value = blnFolder
  980.         End If
  981.         .Update
  982.         .Close
  983.     End With
  984.  
  985. End Sub
  986.  
  987. Public Sub ImportCodeItems(tvw As TreeView)
  988.     '// This routine imports items in the DCB file into the Database
  989.     '// Written By Chris Eastwood
  990.     Dim iFile As Integer
  991.     Dim sFIleName As String
  992.     Dim lCount As Long
  993.     Dim oImport As FileDetails
  994.     Dim sParentKey As String
  995.     Dim sTopParentKey As String
  996.     Dim oColl As Collection
  997.     Dim cHourGlass As CWaitCursor
  998.     Dim lNumCodeItems As Long
  999.     Dim sTmp As String
  1000.     Dim intID As Integer
  1001.     Dim currKey As String
  1002.     Dim intFolderKey As Integer
  1003.  
  1004.     Dim oHeader As FileHeader
  1005.  
  1006.     ' On Error GoTo vbErrorHandler
  1007.  
  1008.     '// Get selected Node
  1009.     Set m_nodNode = tvw.SelectedItem
  1010.     '// If No Node Selected (very unlikely) then exit
  1011.     If m_nodNode Is Nothing Then Exit Sub
  1012.     currKey = m_nodNode.Key
  1013.     '// Get Import File Name
  1014.     sFIleName = frmMain.ShowFileDialog(eOpen, "", "Import Item", "Developers Code Book File|*.dcb")
  1015.     '// If no name selected then quit
  1016.     If Len(sFIleName) = 0 Then Exit Sub
  1017.  
  1018.     '// Get FileHandle
  1019.     iFile = FreeFile
  1020.  
  1021.     '// Get Top Parent Key
  1022.     If m_nodNode.Key = "ROOT" Then
  1023.         sTopParentKey = "0"
  1024.     Else
  1025.         sTopParentKey = Right$(m_nodNode.Key, Len(m_nodNode.Key) - 1)
  1026.     End If
  1027.     '// Set Cursor to HourGlass
  1028.     Set cHourGlass = New CWaitCursor
  1029.     cHourGlass.SetCursor
  1030.     '// Setup Our Collection Internally
  1031.     Set oColl = New Collection
  1032.  
  1033.     '// Place all of the Import into a Transaction for Speed & rollback opportunity
  1034.     BeginTrans
  1035.  
  1036.     '// Open the file
  1037.     Open sFIleName For Binary Access Read As iFile
  1038.  
  1039.     lCount = 1
  1040.  
  1041.     Get #iFile, , oHeader
  1042.  
  1043.     '// Now loop through the records in the file
  1044.     For lCount = 1 To oHeader.lNumberOfRecords
  1045.  
  1046.         '// Get each record until empty
  1047.         Get #iFile, , oImport
  1048.  
  1049.         If oImport.sName = "" Then Exit For
  1050.         If oImport.bFolder Then
  1051.             Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders", dbOpenDynaset)
  1052.             m_rs.AddNew
  1053.             m_rs.Fields("Name").Value = oImport.sName
  1054.             If lCount = 1 Then
  1055.                 intID = 0
  1056.                 intID = sTopParentKey
  1057.                 m_rs!ParentID = intID
  1058.             End If
  1059.             m_rs.Update
  1060.             m_rs.Bookmark = m_rs.LastModified
  1061.             intFolderKey = m_rs!Id
  1062.  
  1063.             oColl.Add Trim$(Str$(intFolderKey)), oImport.sOldID
  1064.  
  1065.             m_rs.Close
  1066.         Else
  1067.             '// Create a new CodeItem for the record
  1068.             Set m_rs = m_db.OpenRecordset("SELECT * FROM Code", dbOpenDynaset)
  1069.             m_rs.AddNew
  1070.             '// Setup the CodeItems values
  1071.             m_rs.Fields("Code").AppendChunk oImport.sCode
  1072.             m_rs.Fields("Description").Value = oImport.sName
  1073.             m_rs.Fields("Example").AppendChunk oImport.sExample
  1074.             m_rs.Fields("Notes").AppendChunk oImport.sNotes
  1075.             If Not NullString(oImport.sVersion) Then m_rs.Fields("Version").Value = "" & oImport.sVersion
  1076.             If Not NullString(oImport.sLevel) Then m_rs.Fields("Level").Value = "" & oImport.sLevel
  1077.             '// If this is the first one, then set it's parent to the selected Node database key
  1078.             If lCount = 1 Then
  1079.                 intID = 0
  1080.                 intID = sTopParentKey
  1081.                 m_rs!ParentID = intID
  1082.             End If
  1083.  
  1084.             '// Add it to the collection - indexed by Original Key
  1085.             '    oColl.Add oKeys, oKeys.sOldID
  1086.             oColl.Add Trim$(Str$(m_rs!Id)), oImport.sOldID
  1087.             '        mval = oColl.Item("201")
  1088.             '// If we're not on the first item to be imported, restructure the items
  1089.             If lCount > 1 Then
  1090.                 sParentKey = intFolderKey
  1091.  
  1092.                 If Len(sParentKey) > 0 And sParentKey <> "0" Then
  1093.                     'rs!ParentID = oColl.Item(sParentKey) '.sNewID
  1094.                     intID = 0
  1095.                     'intID = oColl.Item(sParentKey)
  1096.                     'm_rs!ParentID = intID
  1097.                     m_rs!ParentID = intFolderKey
  1098.                     intID = m_rs!Id
  1099.                 Else
  1100.                     m_rs!ParentID = sTopParentKey
  1101.                 End If
  1102.             End If
  1103.             sParentKey = ""
  1104.             m_rs.Update
  1105.             m_rs.Close
  1106.         End If
  1107.     Next
  1108.     '// Close the file
  1109.     Close iFile
  1110.     '// Commit all of our database work
  1111.     CommitTrans
  1112.     '// Fill the tree with all records from the database
  1113.     FillTree tvw
  1114.     '// Now, get the original Node that was the TopParent, and make sure
  1115.     '// that it's expanded, and visible
  1116.     If Len(sTopParentKey) > 0 And sTopParentKey <> "0" Then
  1117.         Set m_nodNode = tvw.Nodes("C" & sTopParentKey)
  1118.         Set tvw.SelectedItem = m_nodNode
  1119.         m_nodNode.Expanded = True
  1120.         m_nodNode.EnsureVisible
  1121.     End If
  1122.  
  1123.     '// Notify the User of success
  1124.     MsgBox "Imported " & lCount - 1 & " Code Items.", vbInformation, App.ProductName
  1125.  
  1126.     SelectItem currKey, frmMain.ctlData1
  1127.  
  1128.     Exit Sub
  1129.  
  1130. vbErrorHandler:
  1131.     '// Rollback the database work
  1132.     Rollback
  1133.     WriteError Err.Number, Err.Description, "ImportCodeItems", Now, App.Path & "\err.log"
  1134.     MsgBox Err.Description, vbCritical + vbOKOnly, "ImportCodeItems"
  1135.  
  1136. End Sub
  1137.  
  1138. Public Sub SaveSort(strSort As String, intTabIndex As Integer, lv As ListView)
  1139.     Dim blnTab As Boolean
  1140.     Dim intPos As Integer
  1141.     Dim strType As String
  1142.     Dim intID As Integer
  1143.     Dim strVersion As String
  1144.     Dim strLevel As String
  1145.     Dim strValue As String
  1146.     
  1147.     On Error Resume Next
  1148.     
  1149.     '// Saves the current sort mode
  1150.     blnTab = GetSetting(ThisApp, "General", "Remember Tabs", True)
  1151.     If blnTab Then
  1152.         SaveSetting ThisApp, "General", "Control Panel", intTabIndex
  1153.     End If
  1154.  
  1155.     strSort = Right$(strSort, Len(strSort) - 8)
  1156.     intPos = InStr(1, strSort, ":", vbTextCompare)
  1157.     strType = left$(strSort, intPos - 1)
  1158.  
  1159.     If lv.ListItems.Count > 0 Then
  1160.         m_strKey = lv.ListItems(1).Key
  1161.         intID = Right(m_strKey, Len(m_strKey) - 1)
  1162.  
  1163.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID =" & intID, dbOpenSnapshot)
  1164.  
  1165.         strVersion = "" & m_rs!Version
  1166.         strLevel = "" & m_rs!Level
  1167.  
  1168.         m_rs.Close
  1169.     End If
  1170.  
  1171.     If strType = "Version" Then
  1172.         strValue = strVersion
  1173.     End If
  1174.  
  1175.     If strType = "Level" Then
  1176.         strValue = strLevel
  1177.     End If
  1178.  
  1179.     SaveSetting ThisApp, "General", "Sort Type", strType
  1180.     SaveSetting ThisApp, "General", "Sort Value", strValue
  1181.  
  1182. End Sub
  1183.  
  1184. Public Function FindBranch(tvw As TreeView, sText As String, ctl As ctlFavourites, blnMatch As Boolean) As Boolean
  1185.  
  1186.     On Error GoTo vbErrHand
  1187.  
  1188.     Dim intID As Integer
  1189.     Dim lngRet As Long
  1190.     '// Finds an item in the same branch
  1191.     '// Currently only searchs one level down
  1192.     ctl.ClearFindRes
  1193.  
  1194.     m_strKey = tvw.SelectedItem.Key
  1195.     If m_strKey = "ROOT" Then
  1196.         intID = 0
  1197.     Else
  1198.         intID = Right(m_strKey, Len(m_strKey) - 1)
  1199.     End If
  1200.  
  1201.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Code", dbOpenSnapshot)
  1202.  
  1203.     If Not EmptyRS(m_rs) Then
  1204.         m_rs.MoveFirst
  1205.         Do While Not m_rs.EOF
  1206.             If m_rs!ParentID = intID Then
  1207.                 If blnMatch Then '// Match the whole string
  1208.                     If m_rs!Description = sText Then 'Found a match
  1209.                         ctl.AddFindRes sText, "C" & m_rs!Id
  1210.                     End If
  1211.                 Else
  1212.                     lngRet = InStr(1, m_rs!Description, sText, vbTextCompare)
  1213.                     If lngRet > 0 Then '// Found a match
  1214.                         ctl.AddFindRes m_rs!Description, "C" & m_rs!Id
  1215.                     End If
  1216.                 End If
  1217.             End If
  1218.             m_rs.MoveNext
  1219.         Loop
  1220.         m_rs.Close
  1221.     End If
  1222.  
  1223.     If ctl.ListCount > 0 Then
  1224.         ctl.ShowFindTab
  1225.         FindBranch = True
  1226.     Else
  1227.         MsgBox "No Items Found"
  1228.         FindBranch = False
  1229.     End If
  1230.     Exit Function
  1231.  
  1232. vbErrHand:
  1233.     WriteError Err.Number, Err.Description, "FindBranch", Now, App.Path & "\err.log"
  1234.     MsgBox Err.Description, vbCritical + vbOKOnly, "FindBranch"
  1235. End Function
  1236.  
  1237. Public Function FindNotes(sText As String, ctl As Object, tvw As TreeView) As Boolean
  1238.  
  1239.     Dim sMessage As String
  1240.     Dim lRet As Long
  1241.     Dim strID As String
  1242.     '// Finds text in the notes section
  1243.     On Error GoTo vbErrHand
  1244.     FindNotes = False
  1245.     ctl.ClearFindRes
  1246.  
  1247.     Set m_rs = m_db.OpenRecordset("SELECT Notes, ID FROM Code", dbOpenSnapshot)
  1248.     If Not EmptyRS(m_rs) Then
  1249.         m_rs.MoveFirst
  1250.         Do While Not m_rs.EOF
  1251.             lRet = InStr(1, "" & m_rs!Notes, sText, vbTextCompare)
  1252.             If lRet <> 0 Then '// We found a match
  1253.                 strID = "C" & m_rs!Id '// Set the key
  1254.                 ctl.AddFindRes tvw.Nodes(strID), strID 'Add it to the find results
  1255.             End If
  1256.             m_rs.MoveNext
  1257.         Loop
  1258.     End If
  1259.     m_rs.Close
  1260.  
  1261.     If ctl.ListCount > 0 Then
  1262.         sMessage = ctl.ListCount & " item(s) found."
  1263.         FindNotes = True
  1264.     Else
  1265.         sMessage = "No items found."
  1266.     End If
  1267.  
  1268.     MsgBox sMessage
  1269.  
  1270.     ctl.ShowFindTab
  1271.  
  1272.     Exit Function
  1273.  
  1274. vbErrHand:
  1275.     WriteError Err.Number, Err.Description, "FindNotes", Now, App.Path & "\err.log"
  1276.     MsgBox Err.Description, vbCritical + vbOKOnly, "FindNotes"
  1277.  
  1278. End Function
  1279.  
  1280. Public Function FindPartX(sFind As String, ctl As ctlFavourites, tvw As TreeView) As Boolean
  1281.  
  1282.     Dim sMessage As String
  1283.     Dim sRecord As String
  1284.     Dim ret As Integer
  1285.     Dim strID As String
  1286.  
  1287.     On Error GoTo vbErrHand
  1288.  
  1289.     ctl.ClearFindRes
  1290.  
  1291.     Set m_nodNode = tvw.Nodes("ROOT")
  1292.  
  1293.     For Each m_nodNode In tvw.Nodes
  1294.         If InStr(UCase$(m_nodNode.Text), UCase$(sFind)) Then
  1295.             ctl.AddFindRes m_nodNode.Text, m_nodNode.Key
  1296.         End If
  1297.         Set m_nodNode = m_nodNode.Next
  1298.     Next
  1299.  
  1300.     If ctl.ListCount > 0 Then
  1301.         sMessage = ctl.ListCount & " item(s) found."
  1302.         FindPartX = True
  1303.     Else
  1304.         sMessage = "No items found."
  1305.         FindPartX = False
  1306.     End If
  1307.  
  1308.     MsgBox sMessage
  1309.  
  1310.     ctl.ShowFindTab
  1311.  
  1312.     Exit Function
  1313.  
  1314. vbErrHand:
  1315.     WriteError Err.Number, Err.Description, "FindPartX", Now, App.Path & "\err.log"
  1316.     MsgBox Err.Description, vbCritical + vbOKOnly, "FindPartX"
  1317.  
  1318. End Function
  1319.  
  1320. Public Function FindWhole(sText As String, ctl As ctlFavourites, tvw As TreeView) As Boolean
  1321.     Dim sMessage As String
  1322.     Dim strID As String
  1323.  
  1324.     On Error GoTo vbErrHand
  1325.  
  1326.     ctl.ClearFindRes
  1327.  
  1328.     For Each m_nodNode In tvw.Nodes
  1329.         If m_nodNode.Text = sText Then
  1330.             strID = m_nodNode.Key
  1331.             ctl.AddFindRes sText, strID
  1332.         End If
  1333.     Next
  1334.  
  1335.     If ctl.ListCount > 0 Then
  1336.         sMessage = ctl.ListCount & " item(s) found."
  1337.         FindWhole = True
  1338.     Else
  1339.         sMessage = "No items found."
  1340.         FindWhole = False
  1341.     End If
  1342.  
  1343.     MsgBox sMessage
  1344.  
  1345.     ctl.ShowFindTab
  1346.  
  1347.     Exit Function
  1348.  
  1349. vbErrHand:
  1350.     WriteError Err.Number, Err.Description, "FindWhole", Now, App.Path & "\err.log"
  1351.     MsgBox Err.Description, vbCritical + vbOKOnly, "FindWhole"
  1352.  
  1353. End Function
  1354.  
  1355. Private Function NullString(strText As String) As Boolean
  1356.     NullString = False
  1357.     If strText = "" Then NullString = True
  1358.     If strText = vbNullString Then NullString = True
  1359. End Function
  1360.  
  1361. Private Sub HighlightFolders(tvw As TreeView, strKey As String)
  1362.     Dim TVI As TVITEM
  1363.     Dim lngHwnd As Long
  1364.     Dim lngItem As Long
  1365.     Dim lngRet As Long
  1366.     '// Makes a node bold
  1367.     If tvw.Nodes(strKey) Is Nothing Then Exit Sub
  1368.     '// Set the selected item
  1369.     Set tvw.SelectedItem = tvw.Nodes(strKey)
  1370.     '// Get the window handle
  1371.     lngHwnd = tvw.hwnd
  1372.     lngItem = SendMessage(tvw.hwnd, TVM_GETNEXTITEM, TVGN_CARET, 0&)
  1373.     If lngItem <> 0 Then
  1374.         With TVI
  1375.             .hItem = lngItem
  1376.             .mask = TVIF_STATE
  1377.             .stateMask = TVIS_BOLD
  1378.             lngRet = SendMessageAny(tvw.hwnd, TVM_GETITEM, 0&, TVI)
  1379.             .State = TVIS_BOLD
  1380.         End With
  1381.         '// Apply the new style
  1382.         lngRet = SendMessageAny(tvw.hwnd, TVM_SETITEM, 0&, TVI)
  1383.     End If
  1384. End Sub
  1385.  
  1386. Public Property Get DBName() As String
  1387.     DBName = m_strDBName
  1388. End Property
  1389.  
  1390. Public Property Let DBName(ByVal NewName As String)
  1391.     m_strDBName = NewName
  1392. End Property
  1393.  
  1394. Public Sub UpdateKey()
  1395.  
  1396.     Dim intKey As Integer
  1397.     Dim intParentKey As Integer
  1398.  
  1399.     If m_strParentKey = "ROOT" Then
  1400.         intParentKey = "0"
  1401.     Else
  1402.         intParentKey = m_strParentKey
  1403.     End If
  1404.  
  1405.     intKey = m_strKey
  1406.  
  1407.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Code WHERE ID =" & intKey, dbOpenDynaset)
  1408.     m_rs.Edit
  1409.     m_rs!ParentID = intParentKey
  1410.     m_rs.Update
  1411.     m_rs.Close
  1412.  
  1413. End Sub
  1414.  
  1415. Public Property Let ParentKey(ByVal NewKey As String)
  1416.     m_strParentKey = NewKey
  1417. End Property
  1418.  
  1419. 'Public Sub ReWrite(ctl As Object)
  1420. 'Set m_rs = m_db.OpenRecordset("SELECT * FROM Code")
  1421. '
  1422. '    m_rs.MoveFirst
  1423. '    Do While Not m_rs.EOF
  1424. '        frmMain.ctlData1.code = "" & m_rs!code
  1425. '        m_rs.Edit
  1426. '        m_rs!code = "" & ctl.PlainCode
  1427. '        m_rs.Update
  1428. '        m_rs.MoveNext
  1429. '    Loop
  1430. '    m_rs.Close
  1431. '
  1432. '
  1433. 'End Sub
  1434.  
  1435. Public Sub AddFolder(tvw As TreeView, ctl As ctlData)
  1436.     ' Adds a new folder to the db and tree
  1437.     Dim intID As Integer
  1438.  
  1439.     On Error GoTo vbErrHand
  1440.  
  1441.     If tvw.SelectedItem Is Nothing Then Exit Sub
  1442.  
  1443.     If tvw.SelectedItem.Key = "ROOT" Then
  1444.         m_strParentKey = "0"
  1445.     Else
  1446.         m_strParentKey = Right$(tvw.SelectedItem.Key, Len(tvw.SelectedItem.Key) - 1)
  1447.     End If
  1448.  
  1449.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Folders")
  1450.  
  1451.     With m_rs
  1452.         .AddNew
  1453.         .Fields("Name").Value = "New Folder"
  1454.         .Fields("ParentID").Value = m_strParentKey
  1455.         .Update
  1456.         .Bookmark = .LastModified
  1457.         intID = .Fields("ID").Value
  1458.         .Close
  1459.     End With
  1460.  
  1461.     With tvw
  1462.         .Nodes.Add tvw.SelectedItem, tvwChild, "F" & intID, "New Folder", "CLOSED"
  1463.         .Nodes("F" & intID).EnsureVisible
  1464.         .SelectedItem = .Nodes("F" & intID)
  1465.     End With
  1466.  
  1467.     m_strDesc = "New Folder"
  1468.     m_strKey = "F" & intID
  1469.  
  1470.     SelectItem "F" & intID, ctl
  1471.  
  1472.     ctl.CountItems tvw
  1473.  
  1474.     tvw.StartLabelEdit
  1475.  
  1476.     Exit Sub
  1477.  
  1478. vbErrHand:
  1479.     WriteError Err.Number, Err.Description, "AddFolder", Now, App.Path & "\err.log"
  1480.     MsgBox Err.Description, vbCritical + vbOKOnly, "AddFolder"
  1481. End Sub
  1482.  
  1483. Public Function GetVersion() As Long
  1484.     Dim rs As Recordset
  1485.     ' We use a seperate rs var incase it is in use
  1486.     Set rs = m_db.OpenRecordset("SELECT * FROM Version")
  1487.     If Not EmptyRS(rs) Then
  1488.         rs.MoveFirst
  1489.         GetVersion = rs.Fields("DB Version").Value
  1490.     End If
  1491.     rs.Close
  1492.     Set rs = Nothing
  1493. End Function
  1494.  
  1495. Public Sub InserObject(blnFile As Boolean, Optional strPath As String, Optional strLink As String)
  1496.     Dim cHourGlass As CWaitCursor
  1497.     
  1498.     Set cHourGlass = New CWaitCursor
  1499.     cHourGlass.SetCursor
  1500.     
  1501.     If blnFile Then
  1502.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Files")
  1503.  
  1504.         With m_rs
  1505.             .AddNew
  1506.             .Fields("CodeID").Value = Right$(m_strKey, Len(m_strKey) - 1)
  1507.             .Fields("Name").Value = m_strDesc
  1508.  
  1509.             BuildRSFile m_rs, strPath
  1510.  
  1511.             .Fields("DateTime").Value = m_strDate
  1512.             .Update
  1513.             .Bookmark = .LastModified
  1514.             m_strKey = "O" & .Fields("ID")
  1515.             .Close
  1516.         End With
  1517.  
  1518.         DBEngine.Idle dbRefreshCache
  1519.     Else
  1520.         ' Insert a link
  1521.         Set m_rs = m_db.OpenRecordset("SELECT * FROM Links")
  1522.  
  1523.         With m_rs
  1524.             .AddNew
  1525.             .Fields("Link").Value = strLink
  1526.             .Fields("Name").Value = m_strDesc
  1527.             .Fields("DateTime") = Now
  1528.             .Update
  1529.             .Bookmark = .LastModified
  1530.             m_strKey = "L" & .Fields("ID")
  1531.             .Close
  1532.         End With
  1533.     End If
  1534. End Sub
  1535.  
  1536. Private Sub BuildRSFile(rs As Recordset, strPath As String)
  1537.     ' Author: Chris Eastwood - vb@codeguru.com
  1538.     Dim lLen As Long
  1539.     Dim lCount As Long
  1540.     Dim lFragment As Long
  1541.     Dim lChunks As Long
  1542.     Dim bChunk() As Byte
  1543.     Dim iFileNum As Integer
  1544.     Dim oField As Field
  1545.     '
  1546.     ' Copy the File into the recordset field
  1547.     '
  1548.     On Error GoTo vbErrorHandler
  1549.  
  1550.     iFileNum = FreeFile
  1551.     '
  1552.     ' Open the file for binary access so we can read it in chunks
  1553.     '
  1554.     Open strPath For Binary Access Read As iFileNum
  1555.     '
  1556.     ' Get Original Date/Time of the File for storing in the Database
  1557.     '
  1558.     m_strDate = Now
  1559.  
  1560.     lLen = LOF(iFileNum)
  1561.     '
  1562.     ' Get the number of chunks
  1563.     '
  1564.     lChunks = lLen \ CHUNKSIZE
  1565.     '
  1566.     ' Get the small fragment size
  1567.     '
  1568.     lFragment = lLen Mod CHUNKSIZE
  1569.  
  1570.     ReDim bChunk(lFragment)
  1571.  
  1572.     Get iFileNum, , bChunk
  1573.     Set oField = m_rs("File")
  1574.  
  1575.     oField.Value = ""
  1576.     '
  1577.     ' Append the first chunk
  1578.     '
  1579.     oField.AppendChunk bChunk
  1580.  
  1581.     ReDim bChunk(CHUNKSIZE)
  1582.     '
  1583.     ' Now read in the rest of the file into the field
  1584.     '
  1585.     For lCount = 1 To lChunks
  1586.         Get iFileNum, , bChunk()
  1587.         oField.AppendChunk bChunk
  1588.     Next
  1589.     '
  1590.     ' Close the file
  1591.     '
  1592.     Close iFileNum
  1593.  
  1594.     Exit Sub
  1595.  
  1596. vbErrorHandler:
  1597.     Err.Raise Err.Number, Err.Source, Err.Description
  1598. End Sub
  1599.  
  1600. Public Sub EditLink(strID As String, blnDel As Boolean, lv As ListView, Optional strNewLink As String, Optional strNewName As String)
  1601.     ' Edits or removes a link
  1602.  
  1603.     Dim intID As Integer
  1604.  
  1605.     ' Retrieve the link's ID
  1606.     intID = Right$(strID, Len(strID) - 1)
  1607.  
  1608.     ' Select the record
  1609.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Links WHERE ID =" & intID)
  1610.  
  1611.     ' Check if we need to delete the item
  1612.     If blnDel Then
  1613.  
  1614.         With m_rs
  1615.             .Delete
  1616.             .Close
  1617.         End With
  1618.  
  1619.         lv.ListItems.Remove strID
  1620.  
  1621.     Else
  1622.  
  1623.         With m_rs
  1624.             .Edit
  1625.             .Fields("Name").Value = strNewName
  1626.             .Fields("Link").Value = strNewLink
  1627.             .Update
  1628.             .Close
  1629.         End With
  1630.  
  1631.         lv.ListItems(strID).Text = strNewName
  1632.         lv.ListItems(strID).SubItems(1) = strNewLink
  1633.  
  1634.     End If
  1635.  
  1636. End Sub
  1637.  
  1638. Public Sub GetLinks(lv As ListView)
  1639. On Error GoTo vbErrHand
  1640.     
  1641.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Links")
  1642.  
  1643.     If Not EmptyRS(m_rs) Then
  1644.         m_rs.MoveFirst
  1645.  
  1646.         Do While Not m_rs.EOF
  1647.  
  1648.             Set m_liItem = lv.ListItems.Add(, "L" & m_rs!Id, m_rs!Name)
  1649.             m_liItem.SubItems(1) = m_rs!Link
  1650.  
  1651.             m_rs.MoveNext
  1652.         Loop
  1653.  
  1654.         m_rs.Close
  1655.     End If
  1656.     
  1657.     Exit Sub
  1658.  
  1659. vbErrHand:
  1660.     WriteError Err.Number, Err.Description, "GetLinks", Now, App.Path & "\err.log"
  1661.     MsgBox Err.Description, vbCritical + vbOKOnly, "GetLinks"
  1662.  
  1663. End Sub
  1664.  
  1665. Public Sub GetFiles(lv As ListView)
  1666.     Dim strExt As String
  1667.  
  1668.     On Error GoTo vbErrHand
  1669.  
  1670.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Files")
  1671.  
  1672.     If Not EmptyRS(m_rs) Then
  1673.         m_rs.MoveFirst
  1674.  
  1675.         Do While Not m_rs.EOF
  1676.  
  1677.             strExt = Right$(m_rs!Name, 3)
  1678.             Set m_liItem = lv.ListItems.Add(, "O" & m_rs!Id, strExt)
  1679.             m_liItem.SubItems(1) = m_rs!Name
  1680.  
  1681.             m_rs.MoveNext
  1682.         Loop
  1683.  
  1684.         m_rs.Close
  1685.     End If
  1686.     
  1687.     Exit Sub
  1688.  
  1689. vbErrHand:
  1690.     WriteError Err.Number, Err.Description, "GetFiles", Now, App.Path & "\err.log"
  1691.     MsgBox Err.Description, vbCritical + vbOKOnly, "GetFiles"
  1692.  
  1693.  
  1694. End Sub
  1695.  
  1696. Public Sub DeleteFile(lv As ListView)
  1697.     Dim intID As Integer
  1698.  
  1699.     intID = Right$(m_strKey, Len(m_strKey) - 1)
  1700.  
  1701.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Files WHERE ID =" & intID)
  1702.  
  1703.     With m_rs
  1704.         .Delete
  1705.         .Close
  1706.     End With
  1707.  
  1708.     lv.ListItems.Remove m_strKey
  1709. End Sub
  1710.  
  1711. Public Sub ExportToFile(ByVal strFileName As String)
  1712.     '
  1713. ' Export the file from the database to the passed filename
  1714. ' Written by Chris Eastwood - vb@codeguru.com
  1715. '
  1716.     Dim iFileNum As Integer
  1717.     Dim lFileLen As Long
  1718.     Dim lChunks As Long
  1719.     Dim lFragment As Long
  1720.     Dim bChunk() As Byte
  1721.     Dim lCount As Long
  1722.     Dim oField As Field
  1723.     Dim cHourGlass As CWaitCursor
  1724.     
  1725. ' Get the field from the database
  1726.     
  1727.     Set m_rs = m_db.OpenRecordset("SELECT * FROM Files WHERE ID =" & Right$(m_strKey, Len(m_strKey) - 1))
  1728.     
  1729.     If EmptyRS(m_rs) Then Exit Sub
  1730.     
  1731. ' Set the cursor
  1732.     
  1733.     Set cHourGlass = New CWaitCursor
  1734.     cHourGlass.SetCursor
  1735.     
  1736.     iFileNum = FreeFile
  1737.     
  1738. ' Created the named file
  1739.     
  1740.     Open strFileName For Binary Access Write As iFileNum
  1741.     Set oField = m_rs.Fields("File")
  1742.     
  1743. ' Get the length of the file and the number of chunks required
  1744.     
  1745.     lFileLen = oField.FieldSize
  1746.     lChunks = lFileLen \ CHUNKSIZE
  1747.     lFragment = lFileLen Mod CHUNKSIZE
  1748.     
  1749. ' Write away the chunks to the file
  1750.     
  1751.     For lCount = 1 To lChunks
  1752.         ReDim bChunk(CHUNKSIZE)
  1753.         bChunk() = oField.GetChunk(((lCount - 1) * CHUNKSIZE), CHUNKSIZE)
  1754.         Put iFileNum, , bChunk()
  1755.     Next
  1756.     
  1757. ' Write the final (or first if lChunks = 0) chunk
  1758.     
  1759.     ReDim bChunk(lFragment)
  1760.     bChunk = oField.GetChunk(lChunks * CHUNKSIZE, lFragment)
  1761.     
  1762.     Put iFileNum, , bChunk()
  1763.     Close iFileNum
  1764.     
  1765.     m_rs.Close
  1766.     
  1767. ' Tell the user that we have finished
  1768.     
  1769.     MsgBox "Exported file to " & strFileName
  1770. End Sub
  1771.